{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "27451b72-0c2e-4809-8c4a-0923fb7ee22d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3628800\n"
     ]
    }
   ],
   "source": [
    "# 使用while循环进行计算\n",
    "\n",
    "i = 1\n",
    "result = 1\n",
    "while i <= 10:\n",
    "    result *= i   # 简写形式，相当于result = result * i\n",
    "    i += 1    # m每次循环i都要自增1\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e9fe8d04-79f4-4fd3-a495-22a1229dffed",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我要吃美食！\n",
      "我要吃美食！\n",
      "我要吃美食！\n",
      "我要吃美食！\n",
      "我要吃美食！\n",
      "任务结束\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "i = 0\n",
    "while i < 5:\n",
    "    print('我要吃美食！')\n",
    "    i += 1\n",
    "\n",
    "print(\"任务结束\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e336ba76-108b-46d0-88b2-28a94b16430b",
   "metadata": {},
   "source": [
    "##### 使用while计算1+2+3+4+5+...+100累加操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4e26bca7-55b0-4c7a-a2ca-1ea47e93ffa2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5050\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "\n",
    "i = 1\n",
    "result = 0\n",
    "while i <= 100:\n",
    "    result += i   # 每次的值都交给result\n",
    "    i += 1    # 自增\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5c8fc7f1-08ed-4a6c-a41b-284eba2541c5",
   "metadata": {},
   "source": [
    "##### while计算1~100的偶数累加和"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c3ed3e69-3e26-4ae0-91ef-4ebb627287f0",
   "metadata": {},
   "source": [
    "##### 方法一："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a625328b-0534-486d-b17a-4a2080500abf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2550\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "\n",
    "# 方法一： 条件判断和2的余数为0的累加计算\n",
    "i = 1\n",
    "result = 0\n",
    "while i <= 100:\n",
    "    if i % 2 == 0:\n",
    "        result += i\n",
    "    i += 1\n",
    "\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c45ddcc3-1667-4623-8d2f-40c1c373d1d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 方法二："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56b13025-163a-46ac-8385-fd354ed120b1",
   "metadata": {},
   "source": [
    "##### 输出一个斐波那契数列（在1000以内的数值）\n",
    "##### 实际上就是著名的兔子序列，基本结构为1，1，2，3，5，8，13，21，34，55......"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "7b071823-ca09-4d3f-ab80-eb9787a14b45",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1、1、2、3、5、8、13、21、34、55、89、144、233、377、610、987、"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "num_a = 0\n",
    "num_b = 1\n",
    "while num_b < 1000:    # 由于定义的在1000以内的，所给出的条件\n",
    "    print(num_b, end='、')   # 表示每个数字中间使用、进行分隔\n",
    "    num_a, num_b = num_b, num_a + num_b\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8b379949-386e-49fd-b4c2-a06b1419192a",
   "metadata": {},
   "outputs": [],
   "source": [
    "break 和 continue"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c68d0179-20ff-419c-82af-9e9c5cde78a3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "吃了第1个苹果\n",
      "吃了第2个苹果\n",
      "吃了第3个苹果\n",
      "吃饱了不吃了\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "\n",
    "i = 1\n",
    "while i <= 5:\n",
    "    if i == 4:\n",
    "        print('吃饱了不吃了')\n",
    "        break\n",
    "    print(f'吃了第{i}个苹果')\n",
    "    i += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "a0808442-f87e-4c15-8370-abc4850a23c9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "吃了第1个苹果\n",
      "吃了第2个苹果\n",
      "吃到了虫子，第3个苹果\n",
      "吃了第4个苹果\n",
      "吃了第5个苹果\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "\n",
    "i = 1\n",
    "while i <= 5:\n",
    "    if i == 3:\n",
    "        print(f'吃到了虫子，第{i}个苹果')\n",
    "        # 在continue之前一定要修改计数器，否色会陷入死循环\n",
    "        i += 1\n",
    "        continue\n",
    "    print(f'吃了第{i}个苹果')\n",
    "    i += 1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eac11965-b61e-4d1c-bdf2-e2c5f0d4b536",
   "metadata": {},
   "source": [
    "##### for循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2375d13a-bb52-4923-bf1a-df2e04ad4a06",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A\n",
      "B\n",
      "C\n",
      "D\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "words = [\"A\", \"B\", \"C\", \"D\"]\n",
    "for words in words:\n",
    "    print(words)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0011b930-4832-4f15-8cc9-d810aafe5697",
   "metadata": {},
   "source": [
    "##### for遍历字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "ee67367a-186f-4fc6-b225-781ad73efb29",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "字\n",
      "节\n",
      "跳\n",
      "动\n",
      "B\n",
      "y\n",
      "t\n",
      "e\n",
      "D\n",
      "a\n",
      "n\n",
      "c\n",
      "e\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "\n",
    "company = '字节跳动ByteDance'\n",
    "for c in company:\n",
    "    print(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4883a334-27ea-4447-a978-fe89721cc593",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name的值是：duoduo\n",
      "age的值是：22\n",
      "gender的值是：female\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "dict = {'name':'duoduo', 'age': \"22\", 'gender': 'female'}\n",
    "for key in dict:\n",
    "    print(key + '的值是：' + dict[key])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "90e8da94-e6aa-426a-b0d5-88715a1a9b71",
   "metadata": {},
   "source": [
    "##### range函数用于for循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "fe9861ff-8dfb-46f7-b6b7-7ea825def53c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "\n",
    "for i in range(0, 5):     # 没有添加步长的情况下，默认步长为1\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "60e2a0e9-b54a-4fdc-9969-65d142f4d8d3",
   "metadata": {},
   "source": [
    "##### 循环技巧\n",
    "##### 1、items方法\n",
    "##### 在循环遍历字典时，关键字和对应的值可以使用items()方法同时得到"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "cc570dc9-881c-48b9-b611-550278f77ceb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n",
      "b\n",
      "c\n",
      "(610113, '张三')\n",
      "(610114, '李四')\n",
      "(610115, '王五')\n",
      "610113\n",
      "610114\n",
      "610115\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "\n",
    "# 如果要想输出值或者同时输出键和值，就添加values()和item()方法\n",
    "demo_dict = {1:'a', 2:'b', 3:'c'}\n",
    "for item in demo_dict.values():\n",
    "    print(item)\n",
    "\n",
    "demo_dict = {610113:'张三', 610114:'李四', 610115:'王五'}\n",
    "for item in demo_dict.items():\n",
    "    print(item)\n",
    "\n",
    "demo_dict_test = {610113:'张三', 610114:'李四', 610115:'王五'}\n",
    "for itemtest in demo_dict_test:    # 如果不调用任何方法，则只会输出key值\n",
    "    print(itemtest)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "f62e8d8e-2ebb-4d91-b0a5-17df1ecbfdb2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('张三', 1), ('李四', 2), ('王五', 3)]\n",
      "k =  张三 v =  1\n",
      "k =  李四 v =  2\n",
      "k =  王五 v =  3\n"
     ]
    }
   ],
   "source": [
    "# items()方法可以有效的将列表 作为 元组返回\n",
    "dcontent = {'张三':1, '李四':2, '王五':3}\n",
    "\n",
    "dcontent_new = dcontent.items()\n",
    "dict_items = ([('张三',1),('李四',2),('王五',3)])\n",
    "print(dict_items)\n",
    "\n",
    "dcontent_test = {'张三':1, '李四':2, '王五':3}\n",
    "for k,v in dcontent_test.items():\n",
    "    print('k = ', k, 'v = ', v)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0647121d-0ef7-43fe-afa1-2b821c53d9ee",
   "metadata": {},
   "source": [
    "##### 3. 【重要】enumerate函数\n",
    "##### 在循环遍历数据时，索引位置和对应值可以使用enumerate函数统计哪个是得到的内容。\n",
    "##### enumerate 函数会遍历列表中的每个元素，将每个元素的以其索引位置打包成一个元组，并返回这些元组的迭代器\n",
    "##### 它内部会包含迭代器，返回的也是一个迭代器，这个迭代器会生成包含索引和对应元素的元组。\n",
    "##### 从处理的结果看，enumerate函数本身是非常强大的，可以在任何的场景下进行使用，在后期大模型以及深度学习当中，都会出现它的身影。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "65443688-d0a8-41fa-b51a-930de8223ea5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 I\n",
      "1 Love\n",
      "2 Myself\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "\n",
    "for i, v in enumerate([\"I\", \"Love\", \"Myself\"]):\n",
    "    print(i, v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "5205027d-aeed-40f3-93eb-3cf136d37f87",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0, 1122)\n",
      "(1, '张三')\n",
      "(2, '人工智能开发班')\n"
     ]
    }
   ],
   "source": [
    "stu_num = 1122\n",
    "stu_name = '张三'\n",
    "stu_class = '人工智能开发班'\n",
    "dict_test = {stu_num: '1122', stu_name: '张三', stu_class: '人工智能开发班'}\n",
    "for items in enumerate(dict_test):\n",
    "    print(items)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "5412e26b-0e87-4b5b-80dc-a2a93fe7f026",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0, 'apple')\n",
      "(1, 'banana')\n",
      "(2, 'cherry')\n"
     ]
    }
   ],
   "source": [
    "fruits = ['apple', 'banana', 'cherry']\n",
    "for item in enumerate(fruits):\n",
    "    print(item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4417c249-066b-438e-b8a4-287caeeb1c95",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 apple\n",
      "2 banana\n",
      "3 cherry\n"
     ]
    }
   ],
   "source": [
    "# 指定起始索引\n",
    "fruits = ['apple', 'banana', 'cherry']\n",
    "for index, fruit in enumerate(fruits, start=1):    # 指定索引的起始位置，从1开始\n",
    "    print(index, fruit)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "c7ca72f5-e3dd-4226-afbc-545b1b8415b4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(0, 'apple'), (1, 'banana'), (2, 'cherry')]\n"
     ]
    }
   ],
   "source": [
    "# 在列表推导式中使用enumerate\n",
    "fruits = ['apple', 'banana', 'cherry']\n",
    "# 变量for 变量in 容器\n",
    "indexed_fruits = [(index, fruit) for index, fruit in enumerate(fruits)]\n",
    "print(indexed_fruits)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "a705d9f3-7687-4190-893d-216debe04a69",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 h\n",
      "2 e\n",
      "3 l\n",
      "4 l\n",
      "5 o\n",
      "6 A\n",
      "7 I\n",
      "8 _\n",
      "9 W\n",
      "10 o\n",
      "11 r\n",
      "12 l\n",
      "13 d\n"
     ]
    }
   ],
   "source": [
    "# 处理字符串\n",
    "\n",
    "s = \"helloAI_World\"\n",
    "for index, char_test in enumerate(s, start=1):\n",
    "    print(index, char_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3b5eb5d-f204-44b2-be0a-76c3727dd787",
   "metadata": {},
   "source": [
    "##### 4.zip()函数\n",
    "##### 在同时循环遍历两个或多个序列时，可以使用zip()内置函数将这些序列打包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "93fad613-bd98-42bf-96f4-5e22454d0cd2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "学生的姓名是小红\n",
      "学生的年龄是22\n",
      "学生的性别是女\n",
      "学生的职业是入职菜鸟\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "infos = [\"姓名\", \"年龄\", \"性别\", \"职业\"]\n",
    "student = [\"小红\", \"22\", \"女\", \"入职菜鸟\"]\n",
    "# 需要注意的是：这里讲的【打包】不是打压缩包，可以理解为收拾东西，将笔记本电脑和纸质笔记都放进书包里\n",
    "for (i, s) in zip(infos, student):\n",
    "    print(\"学生的{0}是{1}\".format(i, s))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "344de1c2-5c09-4818-8f06-08a01ca11866",
   "metadata": {},
   "source": [
    "##### 5.reversed()函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "e4dfb46a-90f5-4971-8a33-f0318bd89484",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n",
      "3\n",
      "2\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "for i in reversed(range(1,5)):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ace8ed6f-a456-48bc-ac70-d225cdc224c7",
   "metadata": {},
   "source": [
    "##### 6.sorted函数\n",
    "##### 它会生成一个新的排序序列"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d13fb575-ad6b-4f50-81ba-eb4ba1cf26fa",
   "metadata": {},
   "source": [
    "### 列表推导式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "c2874c5c-93c3-44d8-9d41-62c527aa47dc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 4, 9, 16, 25]\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "\n",
    "demo_list = [x*x for x in range(0,6)]\n",
    "print(demo_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "296e9672-71e5-4d4b-b305-a36614e7362c",
   "metadata": {},
   "source": [
    "### 嵌套循环\n",
    "- 嵌套循环是解决普通循环中一些问题而存在的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "7ac81702-cfac-40cc-a571-5e5554741070",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "*****\n",
      "*****\n",
      "*****\n",
      "*****\n",
      "*****\n"
     ]
    }
   ],
   "source": [
    "# 不使用嵌套循环\n",
    "# coding:UTF-8\n",
    "i = 0\n",
    "while i <= 4:\n",
    "    print('*', end='')\n",
    "    i += 1\n",
    "print()\n",
    "\n",
    "a=0\n",
    "while a <= 4:\n",
    "    print('*', end='')\n",
    "    a += 1\n",
    "print()\n",
    "\n",
    "b=0\n",
    "while b <= 4:\n",
    "    print('*', end='')\n",
    "    b += 1\n",
    "print()\n",
    "\n",
    "c=0\n",
    "while c <= 4:\n",
    "    print('*', end='')\n",
    "    c += 1\n",
    "print()\n",
    "\n",
    "d=0\n",
    "while d <= 4:\n",
    "    print('*', end='')\n",
    "    d += 1\n",
    "print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "e6c68bd3-0c97-47fe-81b1-201f64516fe8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "******\n",
      "******\n",
      "******\n",
      "******\n",
      "******\n"
     ]
    }
   ],
   "source": [
    "# 使用嵌套循环\n",
    "# coding:UTF-8\n",
    "j = 0      # 外循环控制行\n",
    "while j <= 4:\n",
    "    i = 0     # 内循环控制列\n",
    "    while i <= 5:\n",
    "        print('*', end='')\n",
    "        i += 1\n",
    "    print()    # 换行\n",
    "    j += 1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "84e9cea1-b6b9-4e78-9298-4bed10eaeedc",
   "metadata": {},
   "source": [
    "通过观察发现，重复的代码执行了5次，那么可以将这些重复的东西放到一个循环中进行处理\n",
    "同理根据打印矩形的操作，也可以打印直角三角形"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "ecc51cce-2b8c-4ecd-98e6-a94fdb4eae7c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "*\n",
      "**\n",
      "***\n",
      "****\n",
      "*****\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "j = 0\n",
    "while j <= 4:\n",
    "    # 一行星星的打印\n",
    "    i = 0\n",
    "    # i表示每行里面星星的个数，这个数字要和行号相等，所以i要和j联动\n",
    "    while i <= j:\n",
    "        print('*', end='')\n",
    "        i += 1\n",
    "    print()\n",
    "    j += 1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "892c2fa4-001c-4386-973f-bd4faa7fb916",
   "metadata": {},
   "source": [
    "打印九九乘法表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "960de6c6-0d78-4780-a477-7d59d304dbc2",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1*1=1\t\n",
      "1*2=2\t2*2=2\t\n",
      "1*3=3\t2*3=3\t3*3=3\t\n",
      "1*4=4\t2*4=4\t3*4=4\t4*4=4\t\n",
      "1*5=5\t2*5=5\t3*5=5\t4*5=5\t5*5=5\t\n",
      "1*6=6\t2*6=6\t3*6=6\t4*6=6\t5*6=6\t6*6=6\t\n",
      "1*7=7\t2*7=7\t3*7=7\t4*7=7\t5*7=7\t6*7=7\t7*7=7\t\n",
      "1*8=8\t2*8=8\t3*8=8\t4*8=8\t5*8=8\t6*8=8\t7*8=8\t8*8=8\t\n",
      "1*9=9\t2*9=9\t3*9=9\t4*9=9\t5*9=9\t6*9=9\t7*9=9\t8*9=9\t9*9=9\t\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "j = 1\n",
    "while j <= 9:\n",
    "    i = 1\n",
    "    while i <= j:\n",
    "        print(f'{i}*{j}={1*j}', end='\\t')\n",
    "        i += 1\n",
    "    print()\n",
    "    j += 1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3661fa3a-4d26-4057-8aac-e8a3982a4aa1",
   "metadata": {},
   "source": [
    "使用for循环打印九九乘法表\n",
    "方式一："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "1ca13ab2-2f68-4ff6-86ab-67c16886b3fc",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1*1=1 \n",
      "2*1=2 2*2=4 \n",
      "3*1=3 3*2=6 3*3=9 \n",
      "4*1=4 4*2=8 4*3=12 4*4=16 \n",
      "5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 \n",
      "6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 \n",
      "7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 \n",
      "8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 \n",
      "9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 \n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "for i in range(1, 10):     # 因为range 不包含最后，所以要写到10，不能写到9\n",
    "    for j in range(1, 10):\n",
    "        if i >= j:\n",
    "            print(\"{}*{}={} \".format(i,j,i*j),end='')\n",
    "    print()\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "22881408-319e-4d67-b5ab-e3e772abb24b",
   "metadata": {},
   "source": [
    "方式二："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "1cc41e31-b7c7-4875-932c-006650792b11",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1*1=1 \n",
      "2*1=2 2*2=4 \n",
      "3*1=3 3*2=6 3*3=9 \n",
      "4*1=4 4*2=8 4*3=12 4*4=16 \n",
      "5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 \n",
      "6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 \n",
      "7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 \n",
      "8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 \n",
      "9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 \n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "for m in range(1, 10):\n",
    "    for n in range(1, m+1):     # 如果是while循环，则这里不用+1，但是range函数不包含最后一个，所以要+1\n",
    "        print(\"{0}*{1}={2} \".format(m,n,(m*n)),end='')\n",
    "    print()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
